<script src="../../router.js"></script>
<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入关键词"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="订购人" prop="buyerName">
        <el-input
          v-model="queryParams.buyerName"
          placeholder="请输入关键词"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="订购人证件号码" prop="ConsigneeCode" label-width="110px">
        <el-input
          v-model="queryParams.ConsigneeCode"
          placeholder="请输入关键词"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="订单时间" prop="orderDate">
        <el-date-picker
          v-model="queryParams.orderDate"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="push_order">
        <el-select v-model="queryParams.pushOrder" placeholder="订单状态" filterable allow-create clearable size="small">
          <el-option
            v-for="(dict,index) in PushOrderOptions"
            :key="index"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload2"
          size="mini"
          :disabled="multiple"
          @click="declare()"
        >批量申报</el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-download"
          size="mini"
          :disabled="multiple"
          @click="select()"
        >批量同步</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-male"
          size="mini"
          :disabled="multiple"
          @click="exportXml()"
        >导出XML报文</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list" style="width: 100%;" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <!--      <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="订单号" align="center" prop="orderId" />
      <el-table-column label="订单日期" align="center" prop="orderDate" width="150" />
      <el-table-column label="订购人" align="center" prop="buyerName" />
      <!--      <el-table-column label="交易流水号" align="center" prop="payOrderNo" :show-overflow-tooltip="true" />-->
      <el-table-column label="订购人证件号" align="center" prop="ConsigneeCode" :show-overflow-tooltip="true" />
      <el-table-column label="订单金额" align="center" prop="realPayment" />
      <el-table-column label="海关订单状态" align="center" :formatter="statusFormat" />
      <el-table-column label="回执信息" align="center" prop="returnInfo" width="200" :show-overflow-tooltip="true" />
      <el-table-column label="详情" align="center" width="50">
        <template v-slot="scope">
          <span style="color: #20a0ff;cursor:pointer" @click="see(scope.row)">查看</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="300">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="primary"
            @click="declare(scope.row,'申报')"
          >申报/变更</el-button>
          <!--          <el-button-->
          <!--            v-if="scope.row.guid !== null && scope.row.guid !== ''"-->
          <!--            size="mini"-->
          <!--            type="success"-->
          <!--            @click="declare(scope.row,'变更')"-->
          <!--          >变更申报</el-button>-->
          <el-button
            size="mini"
            type="success"
            :disabled="scope.row.push_order === 0 ? true : false"
            @click="select(scope.row)"
          >同步回执</el-button>
          <el-button
            size="mini"
            type="warning"
            @click="prints(scope.row)"
          >打印
          </el-button>
          <!--          <el-button type="primary" icon="el-icon-edit" circle @click="edit(scope.row)" />-->
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />
    <!-- 打印 -->
    <Print :open-one.sync="openOne" :orderId="orderId" />

    <!-- 修改 -->
    <el-dialog title="编辑" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" label-width="130px">
        <el-row>
          <el-col :span="8">
            <el-form-item prop="ebpCode">
              <span slot="label">
                <el-tooltip content="电商平台的海关注册登记编号或统一社会信用代码。" placement="top">
                  <i class="el-icon-info" />
                </el-tooltip>
                电商平台代码
              </span>
              <el-input v-model="form.ebpCode" placeholder="请输入电商平台代码" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="电商平台名称" prop="ebpName">
              <el-input v-model="form.ebpName" placeholder="请输入电商平台名称" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="paycomCode">
              <span slot="label">
                <el-tooltip content="支付企业的海关注册登记编号" placement="top">
                  <i class="el-icon-info" />
                </el-tooltip>
                支付企业代码
              </span>
              <el-input v-model="form.paycomCode" placeholder="请输入支付企业代码" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item prop="paycomName">
              <span slot="label">
                <el-tooltip content="支付企业在海关注册登记的企业名称" placement="top">
                  <i class="el-icon-info" />
                </el-tooltip>
                支付企业名称
              </span>
              <el-input v-model="form.paycomName" placeholder="请输入支付企业名称" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="amount">
              <span slot="label">
                <el-tooltip content="商品实际成交价 FOB(不含运杂费)，含非现金抵扣金额" placement="top">
                  <i class="el-icon-info" />
                </el-tooltip>
                商品金额
              </span>
              <el-input v-model="form.amount" placeholder="请输入商品金额" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="distributionfee">
              <span slot="label">
                <el-tooltip content="无则填写“0”" placement="top">
                  <i class="el-icon-info" />
                </el-tooltip>
                运杂费
              </span>
              <el-input v-model="form.distributionfee" placeholder="请输入运杂费" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item prop="discount">
              <span slot="label">
                <el-tooltip content="非现金抵扣金额（使用积分、虚拟货币、代金券等非现金支付金额，无则填写 “0”" placement="top">
                  <i class="el-icon-info" />
                </el-tooltip>
                代扣税款
              </span>
              <el-input v-model="form.discount" placeholder="请输入代扣税款" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="tax">
              <span slot="label">
                <el-tooltip content="企业预先代扣的税款金额，无则填写“0”" placement="top">
                  <i class="el-icon-info" />
                </el-tooltip>
                代扣税款
              </span>
              <el-input v-model="form.tax" placeholder="请输入代扣税款" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="realPayment">
              <span slot="label">
                <el-tooltip content="商品价格+运杂费+代扣税款-非现金抵扣金额，与支付凭证 的支付金额一致" placement="top">
                  <i class="el-icon-info" />
                </el-tooltip>
                实际支付金额
              </span>
              <el-input v-model="form.realPayment" placeholder="请输入实际支付金额" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item prop="contactMobile">
              <span slot="label">
                <el-tooltip content="订购人的交易平台注册号" placement="top">
                  <i class="el-icon-info" />
                </el-tooltip>
                订购人注册号
              </span>
              <el-input v-model="form.contactMobile" placeholder="请输入订购人注册号" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="buyerName">
              <span slot="label">
                <el-tooltip content="订购人的真实姓名" placement="top">
                  <i class="el-icon-info" />
                </el-tooltip>
                订购人姓名
              </span>
              <el-input v-model="form.buyerName" placeholder="请输入订购人姓名" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="ConsigneeCode">
              <span slot="label">
                <el-tooltip content="订购人的身份证件号码" placement="top">
                  <i class="el-icon-info" />
                </el-tooltip>
                订购人证件号码
              </span>
              <el-input v-model="form.ConsigneeCode" placeholder="请输入订购人证件号码" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="订购人电话" prop="contactMobile">
              <el-input v-model="form.contactMobile" maxlength="50" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="payOrderNo">
              <span slot="label">
                <el-tooltip content="支付企业唯一的支付流水号" placement="top">
                  <i class="el-icon-info" />
                </el-tooltip>
                支付交易编号
              </span>
              <el-input v-model="form.payOrderNo" placeholder="请输入支付交易编号" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="收货人姓名" prop="contactName">
              <el-input v-model="form.contactName" placeholder="请输入收货人姓名" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item prop="contactMobile">
              <span slot="label">
                <el-tooltip content="收货人联系电话，必须与电子运单的收货人电话一致" placement="top">
                  <i class="el-icon-info" />
                </el-tooltip>
                收货人电话
              </span>
              <el-input v-model="form.contactMobile" maxlength="50" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="contactAddr">
              <span slot="label">
                <el-tooltip content="必须与电子运单的收货省市区一致" placement="top">
                  <i class="el-icon-info" />
                </el-tooltip>
                收货省市区
              </span>
              <el-input v-model="form.contactAddr" placeholder="请输入收货省市区" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="detailAddr">
              <span slot="label">
                <el-tooltip content="必须与电子运单的收货收货详细地址一致" placement="top">
                  <i class="el-icon-info" />
                </el-tooltip>
                收货详细地址
              </span>
              <el-input v-model="form.detailAddr" placeholder="请输入收货详细地址" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <!--        <el-row>-->
        <!--          <el-col :span="24">-->
        <!--            <el-form-item label="备注">-->
        <!--              <el-input v-model="form.note" type="textarea" />-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->
        <!--        </el-row>-->

        <el-table :data="form.goodsListStr" border stripe style="width: 100%;">
          <el-table-column prop="productName" label="企业商品名称">
            <template slot-scope="scope">
              <el-input v-model="scope.row.productName" :value="scope.row.productName" placeholder="企业商品名称" />
            </template>
          </el-table-column>
          <el-table-column prop="barcode" label="企业商品货号">
            <template slot-scope="scope">
              <el-input v-model="scope.row.barcode" :value="scope.row.barcode" placeholder="企业商品货号" />
            </template>
          </el-table-column>
          <el-table-column prop="productName" label="规格型号">
            <template slot-scope="scope">
              <el-input v-model="scope.row.GModel" :value="scope.row.GModel" placeholder="规格型号" />
            </template>
          </el-table-column>
          <el-table-column prop="barcode" label="条形码">
            <template slot-scope="scope">
              <el-input v-model="scope.row.barcode" :value="scope.row.barcode" placeholder="条形码" />
            </template>
          </el-table-column>
          <el-table-column prop="Unit" label="计量单位">
            <template slot-scope="scope">
              <el-input v-model="scope.row.Unit" :value="scope.row.Unit" placeholder="计量单位" />
            </template>
          </el-table-column>
          <el-table-column prop="Unit" label="原产国">
            <template slot-scope="scope">
              <el-input v-model="scope.row.business_addr" :value="scope.row.business_addr" placeholder="原产国" />
            </template>
          </el-table-column>
          <el-table-column prop="num" label="数量">
            <template slot-scope="scope">
              <el-input v-model="scope.row.num" :value="scope.row.num" placeholder="数量" />
            </template>
          </el-table-column>
          <el-table-column prop="price" label="单价">
            <template slot-scope="scope">
              <el-input v-model="scope.row.price" :value="scope.row.price" placeholder="单价" />
            </template>
          </el-table-column>
          <el-table-column prop="weight" label="净重">
            <template slot-scope="scope">
              <el-input v-model="scope.row.weight" :value="scope.row.weight" placeholder="净重" />
            </template>
          </el-table-column>
        </el-table>
        <el-row style="top: 30px;text-align:center;">
          <el-col :span="24">
            <el-form-item size="large">
              <el-button type="primary" @click="onSubmit">提交</el-button>
              <el-button @click="open === false">取消</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
    <!-- 回执信息-->
    <el-dialog title="回执信息" :visible.sync="dialogVisible" width="30%">
      <span>{{ info }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">关 闭</el-button>
      </span>
    </el-dialog>
    <!-- 查看详情 -->
    <ComOrderDetails :show.sync="show" :form="form" />
  </div>
</template>

<script>
import { list, orderPush, orderSelect, orderInfo, exportOrderXml } from '@/api/order'
import Print from './Print'
export default {
  name: 'Index',
  components: {
    Print
  },
  data() {
    return {
      //打印
      openOne: false,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 非多个禁用
      multiple: true,
      // 是否显示弹出层
      open: false,
      dialogVisible: false,
      // 表单参数
      form: {},
      info: '',
      // 表单校验
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ]
      },
      // 订单状态
      PushOrderOptions: [
        { 'value': 0, 'label': '未推送' },
        { 'value': 1, 'label': '已推送' },
        { 'value': 2, 'label': '电子口岸申报中' },
        { 'value': 3, 'label': '发送海关成功' },
        { 'value': 4, 'label': '发送海关失败' },
        { 'value': 100, 'label': '海关退单' },
        { 'value': 120, 'label': '海关入库' }
      ],
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        orderId: undefined,
        ConsigneeCode: undefined,
        contactName: undefined,
        // 日期范围
        orderDate: [],
        pushOrder: undefined
      },
      show: false,
      ids: [],
      orderId: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询订单详情 */
    see(row) {
      orderInfo({ orderId: row.orderId, type: 1 }).then(response => {
        if (response.code === 10000) {
          this.form = response.data
          this.show = true
        }
      })
    },
    /** 查询订单列表 */
    getList() {
      this.loading = true
      // console.log(this.queryParams)
      list(this.queryParams).then(response => {
        // console.log(response)
        if (response.code === 10000) {
          this.list = response.data
          this.total = response.count
          this.loading = false
        }
      })
    },
    /** 状态字典翻译 */
    statusFormat(row, column) {
      let status = row.push_order
      for (const d of this.PushOrderOptions) {
        if (row.push_order === d.value) {
          status = d.label
          break
        }
      }
      return status
    },
    /** (申报/变更/重新)订单 */
    declare(row, type = '申报') {
      const that = this
      const postIds = row ? row.orderId : this.ids.toString()
      that.$confirm('是否' + type + postIds + '等订单信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        orderPush({ orderId: postIds }).then(response => {
          // console.log(response)
          if (response.code === 10000) {
            that.$message.success(response.message)
          }
          that.getList()
        })
      }).catch(() => {})
    },
    /** 导出xml文件 */
    exportXml() {
      if (this.ids.length === 0) {
        this.$message.error('请勾选订单')
        return
      }
      exportOrderXml({ orderId: this.ids }).then(response => {
        // console.log(response)
        if (response.code === 10000) {
          const url = window.URL.createObjectURL(
            new Blob([response.data], { type: 'text/xml' })
          )
          const link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', 'CEB311Message.xml')
          document.body.appendChild(link)
          link.click()
        }
      })
    },
    /** 修改订单弹窗 */
    edit(row) {
      orderInfo({ orderId: row.orderId, type: 1 }).then(response => {
        // console.log(response)
        if (response.code === 10000) {
          this.form = response.data
          this.open = true
        }
      })
    },
    /** 修改提交 */
    onSubmit() {
      // console.log(this.form)
      orderInfo({ form: this.form, type: 2 }).then(response => {
        // console.log(response)
        if (response.code === 10000) {
          this.$message.success(response.message)
          this.open = false
          this.getList()
        } else {
          this.$message.error(response.message)
        }
      })
    },
    /** 查询回执 */
    select(row) {
      const that = this
      const postIds = row ? row.orderId : this.ids.toString()
      that.$confirm('是否同步' + postIds + '等订单回执?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        orderSelect({ orderId: postIds }).then(response => {
          // console.log(response)
          if (response.code === 10000) {
            if(row){
              that.dialogVisible = true
              that.info = response.data
            }else{
              that.$message.success(response.message)
            }
            that.getList()
          }else {
            that.$message.error(response.message)
          }
        })
      }).catch(() => {})
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        page: 1,
        limit: 10,
        orderId: undefined,
        ConsigneeCode: undefined,
        contactName: undefined,
        startTime: undefined,
        endTime: undefined,
        orderDate: []
      }
      this.handleQuery()
    },
    /** 提交时间改变 */
    datePick(value) {
      if (value) {
        this.queryParams.startTime = value[0]
        this.queryParams.endTime = value[1]
      }
    },
    /** 选中的项 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.multiple = !selection.length
    },
    /** 打印 */
    prints(row) {
      this.orderId = row.orderId
      console.log(this.orderId)
      this.openOne = true
    }
  }
}
</script>
